package com.vcredit.creditcat.time;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.threadpool.RequestLocalThread;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.emall.AlipayProcessService;
import com.vcredit.creditcat.enumtype.EMStatusEnum;
import com.vcredit.creditcat.manager.TimeManager;
import com.vcredit.creditcat.model.AuthAlipay;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;

/**
 * 支付宝认证项结果查询
 */
@JobHander(value="alipayTime")
@Component
public class AlipayTime extends IJobHandler {

	private static Logger log = LoggerFactory.getLogger(AlipayTime.class);

	@Autowired
	private TimeManager<AuthAlipay> timeManager;

	@Autowired
	private AlipayProcessService alipayProcessService;

	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	@Override
	public ReturnT<String> execute(String... strings) throws Exception {
		log.info("支付宝认证定时任务执行了................" + ConcurrentDateUtil.formatDateTime(new Date()));
		RequestLocalThread.create();
		List<AuthAlipay> list = null;
		try {
			list = timeManager.findUnCrawl(AuthAlipay.class);
			if (list == null || list.isEmpty())
				return ReturnT.SUCCESS;
			for (AuthAlipay obj : list) {
				threadPoolTaskExecutor.execute(new CrawleThread(obj));
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ReturnT.FAIL;
		}
		return ReturnT.SUCCESS;
	}

	class CrawleThread implements Runnable {
		private AuthAlipay obj;
		private Logger logger = LoggerFactory.getLogger(CrawleThread.class);

		public CrawleThread(AuthAlipay obj) {
			super();
			this.obj = obj;
		}

		public void run() {
			try {
				BirdResponse crawlStateResult = alipayProcessService.alipayCrawlStatus(obj.getToken());
				if (crawlStateResult.getCrawlStatus() == 2002) { // 采集成功
					if (alipayProcessService.aplipayIsAuth(obj.getToken(), obj.getCust().getCustName(),
							obj.getCust().getCustIdno())) {
						obj.setAuthStatus(EMStatusEnum.AUTH_SUCCESS.getCode());
						obj.setIsReal(1);
						obj.setExpiredTime(ConcurrentDateUtil.formatDateTime(ConcurrentDateUtil.addDayTime(new Date(), 30)));
					} else {
						obj.setAuthStatus(EMStatusEnum.AUTH_FAIL.getCode());
						obj.setIsReal(0);
					}
					obj.setAuthTime(ConcurrentDateUtil.formatDateTime(new Date()));
					timeManager.updateCrawlerState(obj);
				} else if (crawlStateResult.getCrawlStatus() == 2003) {
					obj.setAuthTime(ConcurrentDateUtil.formatDateTime(new Date()));
					obj.setAuthStatus(EMStatusEnum.AUTH_FAIL.getCode());
					timeManager.updateCrawlerState(obj);
				}
			} catch (Exception e) {
				if(e.getMessage().equals("支付宝状态采集失败:无请求")){
					obj.setAuthTime(ConcurrentDateUtil.formatDateTime(new Date()));
					obj.setAuthStatus(EMStatusEnum.AUTH_FAIL.getCode());
					try {
						timeManager.updateCrawlerState(obj);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
				logger.error("Exception occured", e);
			}
		}
	}

}
